/*
 * File Name:               UIHoldButton.cs
 * 
 * Description:             普通类
 * Author:                  lisiyu <576603306@qq.com>

 * Create Date:             2017/06/12
 */

using UnityEngine;
using UnityEngine.EventSystems;


public class UIHoldButton : UnityEngine.UI.Button
{
    public delegate void PressFunc(bool pressed);

    public PressFunc pressFunc;
    public int DELAYFRAMECOUNT = 10;        // 弹起延迟帧数
    private int mCurDelayFrame;             // 当前延迟帧数

    public void addListener(PressFunc callback)
    {
        pressFunc += callback;
    }

    private void FixedUpdate()
    {
        if(mCurDelayFrame >= 0)
        {
            mCurDelayFrame--;
        }

        if(mCurDelayFrame == 0)
        {
            if (pressFunc != null)
            {
                pressFunc(false);
            }
        }
    }

    public override void OnPointerDown(PointerEventData eventData)
    {
        base.OnPointerDown(eventData);
        if (pressFunc != null)
        {
            pressFunc(true);
            mCurDelayFrame = 0;
        }
    }

    public override void OnPointerUp(PointerEventData eventData)
    {
        base.OnPointerUp(eventData);
        mCurDelayFrame = DELAYFRAMECOUNT;
    }

    public override void OnPointerEnter(PointerEventData eventData)
    {
        base.OnPointerEnter(eventData);

        if (pressFunc != null)
        {
            pressFunc(true);
            mCurDelayFrame = 0;
        }
    }

    public override void OnPointerExit(PointerEventData eventData)
    {
        base.OnPointerExit(eventData);
        mCurDelayFrame = DELAYFRAMECOUNT;
    }

    protected override void OnDisable()
    {
        base.OnDisable();
        if (pressFunc != null)
        {
            pressFunc(false);
        }
    }
}